# coding:utf-8
import json
import os
import logging
import datetime

# 查找重复文件
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(name)s - %(levelname)s - %(message)s')
logger = logging.getLogger(__name__)
strftime = datetime.datetime.now().strftime('%Y-%m-%d')
if __name__ == '__main__':
    # 用户路径
    user_dir = os.path.expanduser('~') + os.sep
    # print(user_dir)
    # filePath = user_dir + "Videos"
    filePath = user_dir + "Downloads" + os.sep
    r = {}
    for root, dirs, files in os.walk(filePath):
        for file in files:
            filename, file_type = os.path.splitext(file)
            if file_type == '.ts':
                get = r.get(filename)
                if get is None:
                    get = []
                join = os.path.join(root, file)
                getsize = os.path.getsize(join)
                filesize = getsize / float(1024 * 1024)
                get.append(os.path.join(root, file) + ',size:' + str(round(filesize, 2)))
                r[filename] = get
    file_name = '%s-%s.txt' % ('重复文件', strftime)
    with open(file_name, 'w', encoding='utf-8') as f:
        # 循环写入文件
        # l = list(r.keys())
        i = 0
        for key, value in r.items():

            if len(value) > 1:
                i = i + 1
                # f.write(key + ' : ' + json.dumps(value, ensure_ascii=False) + '\n')
                f.write(key + ' : ' + '\n')
                for item in value:
                    f.write(item + '\n')
        f.write(str(i))
    # print(r)
